<?php

namespace app\admin\logic;

use app\common\exception\LogicException;
use app\common\logic\EncryptLogic;
use app\common\logic\PhoneCodeLogic;
use app\common\logic\RandCreateStrLogic;
use app\model\Admin;

class AdminLogic
{
    /**
     * @Description 获取登录用户信息
     * @author: zj
     * @time 2024/03/05 10:21:33
     * @return mixed
     */
    public function getInfo()
    {
        $token = request()->header('token');
        if (!$token) {
            throw new LogicException('未登录');
        }
        $model = new Admin();
        $userInfo = $model->alias('a')
            ->join('admin_token at','a.id = at.admin_id','left')
            ->field('a.id,a.name,a.phone')
            ->where('at.token',$token)
            ->where('a.delete_time',$model::IS_DELETE_NO)
            ->find();
        if (!$userInfo) {
            throw new LogicException('未登录或登录已过期');
        }

        return $userInfo->toArray();
    }

    /**
     * @Description 找回密码
     * @author: zj
     * @param string $phone
     * @param string $code
     * @param string $new_password
     * @time 2024/03/05 11:07:39
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function findPassword(string $phone,string $code,string $new_password)
    {
        $logic = new PhoneCodeLogic();
        // 判断手机验证码
//        $logic->checkCode($phone,$code);

        $model = new Admin();
        $admin = $model->field('id,name,phone,salt,password')
            ->where('phone',$phone)
            ->where('delete_time',$model::IS_DELETE_NO)
            ->find();
        if (!$admin) {
            throw new LogicException('用户不存在');
        }

        $randLogic = new RandCreateStrLogic();
        $salt = $randLogic->create(4);

        $encrypt = new EncryptLogic();
        $newPwdMd5 = $encrypt->encryptMD5($new_password, $salt);

        $admin->save([
            'salt' => $salt,
            'password' => $newPwdMd5,
            'update_time' => request()->time()
        ]);

        return true;
    }

    /**
     * @Description
     * @author: zj
     * @param int $admin_id
     * @param string $old_password
     * @param string $new_password
     * @time 2024/03/05 11:23:05
     * @return true
     */
    public function modifyPassword(int $admin_id, string $old_password, string $new_password)
    {
        $model = new Admin();
        $info = $model->field('id,name,password,salt')
            ->where('id',$admin_id)
            ->where('delete_time',$model::IS_DELETE_NO)
            ->find();
        if (!$info) {
            throw new LogicException('用户不存在');
        }

        $encrypt = new EncryptLogic();
        $oldPwdMd5 = $encrypt->encryptMD5($old_password, $info['salt']);

        if ($info['password'] != $oldPwdMd5) {
            throw new LogicException('旧密码错误');
        }

        $randLogic = new RandCreateStrLogic();
        $salt = $randLogic->create(4);

        $encrypt = new EncryptLogic();
        $newPwdMd5 = $encrypt->encryptMD5($new_password, $salt);

        $info->save([
            'salt' => $salt,
            'password' => $newPwdMd5,
            'update_time' => request()->time()
        ]);

        return true;
    }
}